<template>
	<jk-dialog
		v-model="showDialog"
		title="选择人员"
		width="900px"
		:append-to-body="true"
		@on-confirm="onConfirmBtn"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
	>
		<div ref="box1" class="flex-end-center margin-bottom-4">
			<el-select v-model="searchParams.postId" placeholder="岗位" clearable size="small" class="search-bar-item-size2 item-gap">
				<el-option v-for="item in postList" :key="item.id" :label="item.name" :value="item.id" />
			</el-select>
			<el-select v-model="searchParams.groupId" placeholder="班组" clearable size="small" class="search-bar-item-size2 item-gap">
				<el-option v-for="item in groupList" :key="item.id" :label="item.name" :value="item.id" />
			</el-select>
			<jk-search-input v-model="searchParams.nameOrCode" placeholder="人员名称" size="small" class="search-bar-item-size2 item-gap" clearable @on-enter="onSearchBtn" />
			<jk-search-button @click="onSearchBtn"></jk-search-button>
		</div>
		<jk-table
			ref="jkTable"
			:data="tableData"
			:loading="tableLoading"
			:height="dialogTableHeight - 60"
			:checkbox-config="{checkMethod: checkboxMethod}"
			@checkbox-change="selectChangeEvent"
		>
			<vxe-column type="checkbox" width="60" align="center"></vxe-column>
			<vxe-table-column field="name" title="姓名" fixed min-width="90"></vxe-table-column>
			<vxe-table-column field="code" title="编号" min-width="80" />
			<vxe-table-column field="postName" title="岗位" min-width="100" />
			<vxe-table-column field="groupName" title="班组" min-width="80" />
			<vxe-table-column field="workshopName" title="区域" min-width="80" />
			<vxe-table-column field="deptName" title="所属部门" min-width="100" />
		</jk-table>

		<footer ref="box2" class="flex-end-center margin-top-10">
			<jk-page
				location="dialog"
				:page-index="searchParams.pageNo"
				:page-sizes="searchParams.pageSizes"
				:page-size="searchParams.pageSize"
				:total="searchParams.count"
				@on-change="onChangePage"
			/>
		</footer>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import { pageConfig } from '../../../../../utils/jk/jk-tool';
    import { workforcemanagementEmpMachinePage } from '../../../../../api/schedule/empMachine';
    import { systemPostPage } from '../../../../../api/system/post';
    import { personnelGroupPage } from '../../../../../api/personnel/group';

    export default {
        name: 'CheckEmpDialog',
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            checkKeyName: {
                type: String,
                default: 'id'
            },
            ruleForm: {
                type: Object,
                default() {
                    return {};
                }
            },
            defaultGroupId: {
                type: Number,
                default: null
            },
            defaultSelectRows: {
                type: Array,
                default() {
                    return [];
                }
            }
        },
        mixins: [calHeight],
        data() {
            return {
                groupList: [],
                postList: [],
                searchParams: {
                    count: 0,
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    nameOrCode: '',
                    status: 5,
                    groupId: null,
                    onJob: true,
                    postId: null,
                    selectItem: true
                },
                processList: [],
                workshopList: [],
                tableLoading: false,
                tableData: [],
                showDialog: false,
                checkArr: []
            };
        },
        methods: {
            onChangePage(pageNo, pageSize) {
                this.$set(this.searchParams, 'pageNo', pageNo);
                this.$set(this.searchParams, 'pageSize', pageSize);
                this.getListRequest();
            },
            checkboxMethod(e) {
                return !e.row._disabled;
            },
            setCheckState(arr) {
                arr.forEach((x) => {
                    x._disabled = false;
                    this.defaultSelectRows.forEach(k => {
                        if (x.id === k[this.checkKeyName]) {
                            x._disabled = true;
                        }
                    });
                });
                this.tableData = arr;
                this.tableData.forEach((x, index) => {
                    if (x._disabled) {
                        this.$refs.jkTable.$refs.xTable.setCheckboxRow([this.tableData[index], this.tableData[index]], true);
                    }
                });
            },
            selectChangeEvent({ records }) {
                this.checkArr = records.filter(x => !x._disabled).map(x => {
                    return x;
                });
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onDeleteBtn(scope) {
                this.tableData.splice(scope.rowIndex, 1);
            },
            onConfirmBtn() {
                if (this.checkArr.length) {
                    const arr = this.checkArr.filter(x => !x._disabled);
                    this.$emit('on-confirm', arr);
                } else {
                    this.$tipsBar(this, 'custom', 'warning', '请选择数据');
                }
            },
            getListRequest() {
                this.tableLoading = true;
                return workforcemanagementEmpMachinePage(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.searchParams.count = res.data.count;
                        this.setCheckState(res.data.res || []);
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            getPostListRequest() {
                return systemPostPage({ auditState: 1 }).then(res => {
                    if (res.data.status === 200) {
                        this.postList = res.data.res;
                    }
                });
            },
            getGroupListRequest() {
                return personnelGroupPage({ auditState: 1 }).then(res => {
                    if (res.data.status === 200) {
                        this.groupList = res.data.res;
                    }
                });
            },
            async getDependentData() {
                this.tableLoading = true;
                this.$set(this.searchParams, 'groupId', this.defaultGroupId);
                await this.getGroupListRequest();
                await this.getPostListRequest();
                await this.getListRequest();
                this.tableLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
<style scoped>
</style>
